<template>
  <div class="main">
    <UnsolvedSearchTools/>
    <el-divider>查询结果</el-divider>
    <el-table
        :data="unsolvedByEid"
        style="width: 100%">
      <el-table-column
          label="日期"
          width="280">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.date }}</span>
        </template>
      </el-table-column>
      <el-table-column
          label="提问者"
          width="280">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.farmer.name }}</span>
        </template>
      </el-table-column>
      <el-table-column
          label="问题描述"
          width="280">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <p>描述: {{ scope.row.question}}</p>
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">查看问题</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)">回复</el-button>
          <el-dialog
              title="回复"
              :visible.sync="dialogVisible"
              width="30%"
              :before-close="handleClose">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
              <el-form-item prop="answer">
                <el-input
                    type="textarea"
                    :rows="2"
                    placeholder="请输入内容"
                    v-model="ruleForm.answer">
                </el-input>
              </el-form-item>
            </el-form>
          <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
        </span>
          </el-dialog>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import UnsolvedSearchTools from "@/components/expert/UnsolvedSearchTools";
import {mapActions, mapState} from "vuex";

export default {
  name: "UnsolvedQuestionList",
  data(){
    return {
      dialogVisible:false,
      ruleForm:{
        id:null,
        answer:''
      },
      rules:{
        answer: [
          { required: true, message: '请填写回复内容', trigger: 'blur' }
        ]
      }
    }
  },
  components:{UnsolvedSearchTools},
  methods: {
    handleEdit(index, row) {
      this.dialogVisible = true
      this.ruleForm.id = row.id
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
    },
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          await this.answerQuestion(this.ruleForm)
          await this.findAllUnsolvedByEid(this.loginExpert.id)
          this.dialogVisible = false
          this.$message.success("回复成功")
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    ...mapActions('question',{findAllUnsolvedByEid:'findAllUnsolvedByEid',answerQuestion:"answerQuestion"}),
    ...mapActions('expert',{findExpertByUid:'findExpertByUid'})
  },
  computed:{
    ...mapState('question',['unsolvedByEid']),
    ...mapState('expert',['loginExpert']),
  },
  async created() {
    await this.findExpertByUid()
    await this.findAllUnsolvedByEid(this.loginExpert.id)
  }
}
</script>

<style scoped>
  .main {
    margin: 20px 100px;
  }
</style>